package kevin.study.zephyr.domain.model.view;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import kevin.study.zephyr.domain.model.Document;
import kevin.study.zephyr.domain.model.Field;
import kevin.study.zephyr.domain.model.Form;
import kevin.study.zephyr.domain.model.Item;


/**
 * view中的一行所代表的值
 * 
 * @author Kevin.Huang
 * @createtime 2007-10-8
 */
public class RowEntity {

	/**
	 * 这一行所对于的Document
	 */
	private Document document;

	/**
	 * 一行中列值和实值的对应
	 */
	private Map< Field, Collection< Object > > fieldKeyValues;

	/**
	 * 一行中列值的名字和实值的对应
	 */
	private Map< String, Collection< Object > > stringKeyValues;

	public RowEntity(Document document, Collection< Field > fields) {
		super();
		this.document = document;
		this.fieldKeyValues = new HashMap< Field, Collection< Object > >();
		this.stringKeyValues = new HashMap< String, Collection< Object > >();
		if (document.getItems() != null && !document.getIndexItems().isEmpty()) {
			for (Item item : document.getItems()) {
				if (fields.contains(item.getField())) {
					fieldKeyValues.put(item.getField(), item.getValues());
					stringKeyValues.put(item.getField().getName(), item.getValues());
				}
			}
		}
	}

	/**
	 * 取得文档的UUID
	 * 
	 * @return
	 */
	public String getDocumentUUID() {
		return this.document.getUuid();
	}

	/**
	 * 取得父文档的UUID
	 * 
	 * @return
	 */
	public String getDocumentREFID() {
		if (this.document.getParentDocument() == null) {
			return null;
		} else {
			return this.document.getParentDocument().getUuid();
		}
	}

	/**
	 * 取得Document所对应的Form
	 * 
	 * @return
	 */
	public Form getDocumentForm() {
		return this.document.getForm();
	}

	/**
	 * 取得Document所对应的Form名
	 * 
	 * @return
	 */
	public String getDocumentFormName() {
		return this.document.getForm().getName();
	}

	/**
	 * 根据Field取值
	 * 
	 * @param field
	 * @return
	 */
	public Collection< Object > getValueByField(Field field) {
		return this.fieldKeyValues.get(field);
	}

	/**
	 * 根据Field的名字取值
	 * 
	 * @param fieldName
	 * @return
	 */
	public Collection< Object > getValueByFieldName(String fieldName) {
		return this.stringKeyValues.get(fieldName);
	}

	/**
	 * @return the document
	 */
	public Document getDocument() {
		return document;
	}

}
